<?php
	///////////////////////////////////////////////////////////////////////////////////////////////////
	//	TIME FUNCTIONS
	//	WRITTEN BY: TRAVIS THORNTON
	//	12-07-2009
	//
	//	What this page does:
	//	1) Stores globals of the time in a given 2 week period
	//	2) Calculates the time in a 2 week period given function
	//	3) Returns hours in several ways
	//
	// 	Future work:
	//	1) Currently, time is being calculated per event day. This does not allow night to next day transitions. 
	//       The variables needed for this are calculated in payperiod_includes, I just didn't have enough time. 
	////////////////////////////////////////////////////////////////////////////////////////////////////

//global hour counters
$total_hours = 0;
$total_ot = 0;
$total_double = 0;
$total_bonus = 0;

//this function will get the total time from the passed in array and save to globals as well as returning a string
function getTotals($time_array)
{
	//set inital values
	$total_hour = 0;
	$last_event = 1;
	$last_hour = 0;
	$last_min = 0;
	$bonus_time = 0;
	
	//use globals
	global $total_hours;
	global $total_ot;
	global $total_double;
	global $total_bonus;
	
	//output
	$output;
	
	//go through the array
	for($u = 0; $u < count($time_array); ++$u)
	{
		//trun the data into an array for event info
		$data = explode("@", $time_array[$u]);
		
		//if it is bonus time, save it (it is stored in mins only)
		if((int)$data[0] == 4 )
		{
			$bonus_time = $bonus_time + round((int)$data[1] / 60, 2);//set it to hours instead of mins
		}
		else
		{
			//exploed the data futher for time info
			$date = explode(" ", $data[1]);
			$time = explode(":", $date[1]);
			$hour = (int)$time[0];
			$min = (int)$time[1];
			
			//calculate the time
			switch((int)$data[0])
			{
				case 0:
					if($last_event == 1)//can only sign on if the last event was a sign off
					{
						$last_event = 0;
						$last_hour = $hour;
						$last_min = $min;
					}else{
						return "Error in this column";
					}
				break;
				case 1:
					if($last_event == 0 || $last_event == 3)//can only log out if back from lunch or logged in
					{
						$last_event = 1;
						$total_hour += round(($hour - $last_hour) + (($min - $last_min)/60), 2);
					}else{
						return "Error in this column";
					}
				break;
				
				case 2:
				if($last_event == 0 || $last_event == 3)//can only go to lunch if back from lunch or logged in
					{
						$last_event = 2;
						$total_hour += round(($hour - $last_hour) + (($min - $last_min)/60), 2);
					}else{
						return "Error in this column";
					}
				break;
				case 3:
				if($last_event == 2)//can only come back from lunch if went to lunch
					{
						$last_event = 3;
						$last_hour = $hour;
						$last_min = $min;
					}else{
						return "Error in this column";
					}
				break;
			}
		}
	}

	//update the total bonus time
	$total_bonus += $bonus_time;
	
	//update the total times and calc ot and double time
	if($total_hour > 8)
	{
		$total_hours += 8;
		$output = (8 + $bonus_time) . " Hours<br/>\n";
		
		$total_hour -= 8;
		
		if($total_hour > 4)
		{	
			$total_hour -= 4;
			$total_ot += 4;
			$total_double += $total_hour;
			$output = $output . "4 Hours Overtime<br/>\n" . $total_hour . " Hours Double Time\n";
		}else{
			$total_ot += round($total_hour, 2);
			$output = $output . round($total_hour, 2) . " Hours Overtime\n";
		}
		
	}else{
		$total_hours += $total_hour;
		$output = ($total_hour + $bonus_time) . " Hours\n";
	}
	
	//>40 hors/week = ot
	if( $total_hours > 40 )
	{
		$total_ot += ($total_hours - 40)
		$total_hours = 40;
	}
	
	//return the output
	return $output;
}

//This will generate the 2 week total time
function generate2WeekTotals($user, $period_start)
{
	require_once 'php_includes.php';
	
	//use globals
	global $total_hours;
	global $total_ot;
	global $total_double;
	global $total_bonus;
	
	sqlConnect();
	
	//get the correct pay period
	$sql = 'select Entry1, Entry2, Entry3, Entry4, Entry5, Entry6, Entry7, Entry8, Entry9, Entry10, Entry11, Entry12, Entry13, Entry14 from PayPeriod where EmployeeID = \'' . $user . '\' AND PeriodStart LIKE(\'' . $period_start . '\')';
			
	// Perform Query
	$sqlresult = sqlQuery($sql);
	
	sqlExit();
	
	
	if($sqlresult)
	{	
		//get the day events put into a array, $day
		$day[0] = explode("|", mysql_result($sqlresult, 0, 0));
		$day[1] = explode("|", mysql_result($sqlresult, 0, 1));
		$day[2] = explode("|", mysql_result($sqlresult, 0, 2));
		$day[3] = explode("|", mysql_result($sqlresult, 0, 3));
		$day[4] = explode("|", mysql_result($sqlresult, 0, 4));
		$day[5] = explode("|", mysql_result($sqlresult, 0, 5));
		$day[6] = explode("|", mysql_result($sqlresult, 0, 6));
		$day[7] = explode("|", mysql_result($sqlresult, 0, 7));
		$day[8] = explode("|", mysql_result($sqlresult, 0, 8));
		$day[9] = explode("|", mysql_result($sqlresult, 0, 9));
		$day[10] = explode("|", mysql_result($sqlresult, 0, 10));
		$day[11] = explode("|", mysql_result($sqlresult, 0, 11));
		$day[12] = explode("|", mysql_result($sqlresult, 0, 12));
		$day[13] = explode("|", mysql_result($sqlresult, 0, 13));
		
		//foreach week
		for($y = 0; $y < 2; ++$y)
		{
			//calc per day
			for($i = 0; $i < 7; ++$i)
			{	
				//get the total and pass the day data to the function
				getTotals($day[$i + (7 * $y)]);
			}
		}
	}
}

//returns total hours for the 2 weeks
function getBasic2WeekTotal()
{
	//use globals
	global $total_hours;
	global $total_ot;
	global $total_double;
	global $total_bonus;
	
	//add them up
	return $total_hours + $total_ot + $total_double + $total_bonus;
}


///These functions were used for something, but they were changed to use the global instead
//Get total hours
function getHours()
{
	global $total_hours;
	
	return $total_hours;
}

//Get total ot hours
function getOTHours()
{
	global $total_ot;
	
	return $total_ot;
}

//Get total double hours
function getDoubleHours()
{
	global $total_double;
	
	return $total_double;
}

//Get total bonus hours
function getBonusHours()
{
	global $total_bonus;
	
	return $total_bonus;
}

function resetGlobalTime()
{
	global $total_hours;
	global $total_ot;
	global $total_double;
	global $total_bonus;
	
	$total_hours = 0;
	$total_ot = 0;
	$total_double = 0;
	$total_bonus = 0;
}
?>

